from .llm import *

class AiCore:
    def __init__(self, audio_prompt, ai_core_prompt):
        self.audio_prompt = audio_prompt
        self.ai_core_prompt = ai_core_prompt

    def _system_messages(self):
        system_messages = {"role": "system", "content": self.audio_prompt + '\n' + self.ai_core_prompt}
        return system_messages

    def _user_messages(self, user_messages):
        user_messages = {"role": "user", "content": user_messages}
        return user_messages

    def run(self, user_messages, model_name):
        messages = []
        messages.append(self._system_messages())
        messages.append(self._user_messages(user_messages))

        tool_call = False
        tool_message = MessageContent(role="assistant")
        for delta in deepseek(messages=messages, model=model_name):
            if isinstance(delta.content, str):
                content = delta.content
                yield MessageContent(role="assistant", content=content)
            elif isinstance(delta.reasoning_content, str):
                yield MessageContent(role="assistant", reasoning_content=delta.reasoning_content)
            elif isinstance(delta.tool_calls, list):
                tool_call = True
                function = delta.tool_calls[0].function
                if function and function.name:
                    tool_message.tool_calls = [delta.tool_calls[0]]
                if function and function.arguments:
                    tool_message.tool_calls[0].function.arguments += function.arguments

        if tool_call:
            yield tool_message